<?php
/**
 * Created by PhpStorm.
 * User: ycbwl
 * Date: 2018/10/23
 * Time: 10:18 PM
 */
namespace app\api\validate;

use app\lib\exception\ParameterException;
use think\Validate;

class OrderPlace extends BaseValidate{

    protected $rule = [
        'products' => 'require|checkProducts'
    ];

    protected $message = [
    ];

    protected $singleRule = [
        'product_id' => 'require|isPositiveInteger',
        'count' => 'require|isPositiveInteger'
    ];

    protected function checkProducts($values){
        if(!is_array($values)){
            throw  new  ParameterException([
                'msg'=>'商品参数不正确'
            ]);
        }
        if(empty($values)){
            throw new ParameterException([
                'msg'=>'商品列表不能为空'
            ]);
        }
        foreach ($values as $value){
            $this->checkProduct($value);
        }
        return true;
    }


    protected function checkProduct($value){
        $validate = new OrderArray();
        $result = $validate->check($value);
        if(!$result){
            throw new ParameterException([
                'msg'=>'商品数据参数错误'
            ]);
        }
    }

}